package com.iccaplayer;

import com.iccaplayer.sequencer.Sequencer.OnBPMListener;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class ProgressBarView extends View implements OnBPMListener {
	private static final String TAG = "ProgressBarView";
	
    private static final int BAR_WIDTH = 52;
    private static final int BTN_SPACE = 2;
    private static final int PROGRESS_SIZE = 50;

    ShapeDrawable progressBar;
    ProgressBarView thisView = this;
    Handler progressHandler;

    int totalWidth, barHeight, currentBarXPos, beatLength;
    boolean isDraw;

    private static int barColor = Color.argb(100, 251, 21, 76);
    private static final int BAR_TRANSPARENCY = 200;

    long updateDelay, beatTime;

    Runnable progressRunnable = new Runnable() {

        @Override
        public void run() {
            thisView.invalidate();
            moveBar();
        }
    };

    /**
     * @param context
     * @param width sequencer board total width
     * @param height sequencer board total height
     * @param beatLength beat length in pixels
     */
    public ProgressBarView(Context context, int width, int height, int beatLength, int bpm) {
        super(context);
        progressHandler = new Handler();

        barHeight = height;
        totalWidth = width;
        currentBarXPos = 1;

        // How long it takes to go through a beat
        this.beatTime = (60 * 1000) / bpm;
        this.beatLength = beatLength + BTN_SPACE;

        int nCallsToGoThroughABeat = beatLength / PROGRESS_SIZE;
        this.updateDelay = beatTime / nCallsToGoThroughABeat;

        progressBar = new ShapeDrawable(new RectShape());
        progressBar.getPaint().setColor(barColor);
        progressBar.setAlpha(BAR_TRANSPARENCY);
//        moveBar();
    }
    
    public void resetBarSize(int width, int height){
    	totalWidth = width;
    	barHeight = height;
    }
    
    public void showBar(boolean draw){
    	isDraw = draw;
    }

    public void moveBar() {
    	synchronized (progressBar) {        
    		currentBarXPos = (currentBarXPos + PROGRESS_SIZE + BTN_SPACE) + 2;        
    		progressBar.setBounds(currentBarXPos - BAR_WIDTH, 0, currentBarXPos, barHeight);
    	}
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        synchronized (progressBar) {
        	if(isDraw)
        		progressBar.draw(canvas);
        }
    }

    @Override
    public void onBPM(int beatCount, boolean isPlaying) {
    	currentBarXPos = beatCount * beatLength + (beatCount/4)*BTN_SPACE;    	
    	progressHandler.removeCallbacks(progressRunnable);
    	for (int i = 0; i < beatTime; i += updateDelay) {
    		progressHandler.postDelayed(progressRunnable, i);
    	}
    }
}
